package top.italks.manufactureproject.ui.rencai;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import top.italks.manufactureproject.constant.Config;
import top.italks.manufactureproject.R;
import top.italks.manufactureproject.ui.rencai.adapter.TalentAdapter;
import top.italks.manufactureproject.ui.rencai.bean.PeopleGetAll;
import top.italks.manufactureproject.ui.rencai.bean.PeopleGetAll.DataBean;
import top.italks.manufactureproject.utils.HttpUtils;

//人才市场
public class TalentMarketActivity extends AppCompatActivity {

    private TalentAdapter talentAdapter;
    @BindView(R.id.listView)
    ListView listView;

    @BindView(R.id.status)
    TextView status;

    @BindView(R.id.gold)
    TextView gold;

    private boolean statusUp = true;
    private boolean goldUp = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talent_market);
        ButterKnife.bind(this);
        getInfo();

        talentAdapter = new TalentAdapter(this);
        listView.setAdapter(talentAdapter);
    }

    @OnClick({R.id.status
        , R.id.gold})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.status:
                statusUp = !statusUp;
                if (statusUp) {
                    status.setText("类型(升序)");
                } else {
                    status.setText("类型(降序)");
                }
                Collections.sort(talentAdapter.getPeopleList(), new StatusComparator());
                talentAdapter.notifyDataSetChanged();
                break;
            case R.id.gold:
                goldUp = !goldUp;
                if (goldUp) {
                    gold.setText("薪资(升序)");
                } else {
                    gold.setText("薪资(降序)");
                }
                Collections.sort(talentAdapter.getPeopleList(), new GoldComparator());
                talentAdapter.notifyDataSetChanged();
                break;
        }
    }


    private void getInfo() {
        HttpUtils.post(Config.url + "/dataInterface/People/getAll"
            , null, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    PeopleGetAll peopleGetAll = new Gson().fromJson(json, PeopleGetAll.class);

                    //排序
                    Collections.sort(peopleGetAll.getData(), new StatusComparator());

                    talentAdapter.setPeopleList(peopleGetAll.getData());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            talentAdapter.notifyDataSetChanged();
                        }
                    });
                }
            });
    }

    private class StatusComparator implements Comparator<PeopleGetAll.DataBean> {

        @Override
        public int compare(DataBean o1, DataBean o2) {
            if (o1.getStatus() < o2.getStatus()) {
                if (statusUp) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (o1.getStatus() > o2.getStatus()) {
                if (statusUp) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                return 0;
            }
        }
    }

    private class GoldComparator implements Comparator<PeopleGetAll.DataBean> {

        @Override
        public int compare(DataBean o1, DataBean o2) {
            if (o1.getGold() < o2.getGold()) {
                if (goldUp) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (o1.getGold() > o2.getGold()) {
                if (goldUp) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                return 0;
            }
        }
    }
}
